package com.bems.action;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bems.jdbc.common.DBManager;
import com.bems.pojo.POJO;
import com.bems.pojo.Type;
import com.bems.pojo.Ware;
import com.bems.service.WareService;
import com.bems.util.PageUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import cn.org.ape.BaseAction;

public class WareAction extends BaseAction 
{
		private String type_id;
		private int limit;
		private int page;
		
		WareService wareService = new WareService();
		
		public void list() 
		{
			Map<String, Object> map = new HashMap<String, Object>();
			PageUtil pageutil = wareService .list(type_id, page, limit);
			
			JsonObject jsonObject = new JsonObject();
			jsonObject.addProperty("totalCount", pageutil.getTotalpage());
			Gson gson = new Gson();
			jsonObject.add("resultList", gson.toJsonTree(pageutil.getList()));
			DBManager.closeConnection();
			printJson(jsonObject.toString());
		}
		
		public void listtype() 
		{	
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("type_id", type_id);
						List<Ware> list = (List<Ware>) new Ware().getlist(map );
						Gson gson = new Gson();
						JsonElement element = gson.toJsonTree(list);	
						DBManager.closeConnection();
						printJson(element.toString());
		}
		
		 public void listall() 
			{	
							List<Ware> list = (List<Ware>) new Ware().list();
							Gson gson = new Gson();
							JsonElement element = gson.toJsonTree(list);	
							DBManager.closeConnection();
							printJson(element.toString());
			}
		
		 public void count() 
		 {
			 
			 JsonObject jsonObject = new JsonObject();
			 List<Type> lists = (List<Type>) new Type().list();
			 DecimalFormat format = new DecimalFormat("#.00");
			 for (Type type : lists) {
				 jsonObject.addProperty("type_id"+type.getId(),format.format(wareService.getTotal(type.getId()+"")));
			}
			 
				DBManager.closeConnection();
				printJson(jsonObject.toString());
		}
		
		
		

		public String getType_id() {
			return type_id;
		}

		public void setType_id(String type_id) {
			this.type_id = type_id;
		}

		public int getLimit() {
			return limit;
		}

		public void setLimit(int limit) {
			this.limit = limit;
		}

		public int getPage() {
			return page;
		}

		public void setPage(int page) {
			this.page = page;
		}
		
}
